var robot = require("robotjs");
M.ipLock=false;

sseApp=M.sseServer()

app.begin((req,res)=>{

    console.log(req.url)
})



app.get("ipinfo",(req,res)=>{
    console.log(req.params)
    M.ipInfo=req.params;
    M.ipLock=false
    res.send(M.result())
})


app.get("getIpInfo",async (req,res)=>{
    if(M.ipLock==false){
        M.ipLock=true
        M.ipInfo="稍等一下01"
        await getIpInfo(req.params.ip)
        await delayMs(100)
        M.ipLock=false
        res.send(M.result(M.ipInfo))
        return
    }
    res.send(M.result("稍等一下02"))
})



app.get("/sseServer",sseApp)
app.get("/getById",(req,res)=>{
    console.log(req.params);
    sseApp.send("AAAAAAAAAAA");
    res.send("ok");
})

async function delayMs(ms){
    return new Promise(r=>{
        setTimeout(()=>{
            r(1)
        },ms)
    })
}


async function getIpInfo(ip){
        M.log("start-----------")
        sseApp.send(`document.querySelector(".acSearchTool__searchInput-Exa1W").value=""`);
        await delayMs(100)
        robot.setMouseDelay(0);
        robot.moveMouse(367, 373);
        robot.mouseClick()
        await delayMs(100)
        robot.typeString(ip+"");
        robot.moveMouse(907, 373);
        robot.mouseClick()
        await delayMs(500)
         sseApp.send(`M.ip= document.querySelectorAll(".qifu__ipResultRow-2Aibv")[0].innerText;`);
         sseApp.send(` M.address= document.querySelectorAll(".qifu__ipResultRow-2Aibv")[1].innerText;`);
        sseApp.send(`  M.isp= document.querySelectorAll(".qifu__ipResultRow-2Aibv")[2].innerText;`);

        sseApp.send(`console.log("ww");M.fetchGet("http://localhost:8888/ipinfo?a="+M.ip+"&b="+M.address+"&c="+M.isp)`);



        M.log("end-----------")
}